package com.ocm.product.controller;

import com.ocm.product.service.ICartService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import com.ocm.core.secure.BladeUser;
import com.ocm.core.tool.api.R;
import com.ocm.product.dto.CartDTO;
import com.ocm.product.dto.CartUpdateDTO;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author hqy
 * @Date 13/01/2022
 * @Description
 */
@RestController
@AllArgsConstructor
@RequestMapping("/cart")
@Api(value = "", tags = "接口")
public class CartController {

	private final ICartService cartService;

	@PostMapping
	public R addCart(@RequestBody CartDTO dto, BladeUser bladeUser){

		cartService.addCart(dto.getSkuId(),dto.getSum(),bladeUser.getUserId());
		return R.success("操作成功");
	}

	@PostMapping("/delete")
	public R deleteCart(@RequestBody  List<Long> ids, BladeUser bladeUser){
		cartService.delCart(ids,bladeUser.getUserId());
		return R.success("操作成功");
	}

	@GetMapping("/count")
	public R<Long> countCart(BladeUser bladeUser){
		return R.data(cartService.countCart(bladeUser.getUserId()));
	}

	@GetMapping
	public R getCart(BladeUser bladeUser){
		return R.data(cartService.getCart(bladeUser.getUserId()));
	}

	@PostMapping("/update")
	public R updateCart(@RequestBody CartUpdateDTO dto, BladeUser user){
		cartService.updateCart(dto,user.getUserId());
		return R.success("操作成功");
	}
}
